//
//  DHFeaturedCell.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/4/16.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHFeaturedCell.h"
#import "DHFeaturedCollectionCell.h"
#import "DHS2_ProductDetailController.h"

@interface DHFeaturedCell()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (strong, nonatomic) NSArray *itemArr; //item数组

@end

@implementation DHFeaturedCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    WeakSelf(weakSelf)
    self.moreButton.layer.cornerRadius = self.moreButton.height / 2;
    [self.moreButton bk_addEventHandler:^(id sender) {
        [weakSelf.viewController.tabBarController setSelectedIndex:2];
    } forControlEvents:UIControlEventTouchUpInside];
    
    self.collectionView.delegate = self;
    self.collectionView.dataSource = self;
    [self.collectionView registerNib:[UINib nibWithNibName:@"DHFeaturedCollectionCell" bundle:nil] forCellWithReuseIdentifier:kDHFeaturedCollectionCell];
}

- (NSArray *)itemArr{
    if (!_itemArr) {
        _itemArr = [NSArray array];
    }
    return _itemArr;
}

- (void)updateCellWithArray:(NSArray *)arr{
    self.itemArr = arr;
    [self.collectionView reloadData];
}

#pragma mark - UICollectionViewDataSource and UICollectionViewDelegate
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    return self.itemArr.count;
}

// The cell that is returned must be retrieved from a call to -dequeueReusableCellWithReuseIdentifier:forIndexPath:
- (__kindof UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    DHFeaturedCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:kDHFeaturedCollectionCell forIndexPath:indexPath];
    NSDictionary *model = [self.itemArr objectAtIndex:indexPath.row];
    [cell.imageView dh_setImageURLString:[[model objectForKey:@"snapshots"] objectAtIndex:0]];
    cell.nameLabel.text = [[model objectForKey:@"brand"] objectForKey:@"name"];
    cell.titleLabel.text = [model objectForKey:@"name"];
    cell.priceLabel.text = [NSString stringWithFormat:@"¥%@ / ¥%@",[model objectForKey:@"discount_price"],[model objectForKey:@"sales_price"]];
    cell.lineViewLeftLayout.constant = cell.priceLabel.width / 2 + 5;
    cell.lineViewWidthLayout.constant = [[NSString stringWithFormat:@"%@",[model objectForKey:@"sales_price"]] widthForFont:cell.priceLabel.font] + 6;
    return cell;
}

#pragma mark ---- UICollectionViewDelegateFlowLayout
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    return (CGSize){(kScreenWidth - 16 - 8)/3,190};
}

// 两行之间的最小间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section{
    return 0;
}

// 两个cell之间的最小间距，是由API自动计算的，只有当间距小于该值时，cell会进行换行
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section{
    return 4;
}

- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    NSDictionary *model = [self.itemArr objectAtIndex:indexPath.row];
    DHS2_ProductDetailController *vc = [[DHS2_ProductDetailController alloc] initWithProduct:model];
    vc.hidesBottomBarWhenPushed = YES;
    [self.viewController.navigationController pushViewController:vc animated:YES];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
